package bit.lotterysystem.common.intercepter;

import bit.lotterysystem.common.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 同意拦截
 */
@Component
@Slf4j
public class LoginIntercepter implements HandlerInterceptor {
    /**
     * 业务请求前调用
     * @param request current HTTP request
     * @param response current HTTP response
     * @param handler chosen handler to execute, for type and/or instance evaluation
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求头
        String token=request.getHeader("user_token");
        log.info("获取token:{}",token);
        log.info("获取路径：{}",request.getRequestURI());
        //令牌解析
        Claims claims = JWTUtil.parseJWT(token);
        if(claims==null){
            log.error("解析令牌失败！");
            response.setStatus(401);
            return false;
        }
        log.info("解析令牌成功！");
        return true;

    }
}
